package com.cloudspace.jindun.activity.Woyao;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.model.UserInfoResult;
import com.cloudspace.jindun.network.url.YXHttpUrl;
import com.cloudspace.jindun.user.UserSessionManager;
import com.cloudspace.jindun.utils.ToastMaker;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class ResetPasswordActivitye extends CustomTitleBaseActivity {
    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.restPass_et_01)
    EditText mRestPassEt01;
    @Bind(R.id.restPass_et_02)
    EditText mRestPassEt02;
    @Bind(R.id.restPass_bt_qd)
    Button mRestPassBtQd;
    private String text01;
    private String text02;
    private static final int PASS_CORRECT = 1;//密码正确
    private static final int PASS_FAILED = 2;//密码失败

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetpassword, false);
        ButterKnife.bind(this);
    }


    @OnClick({R.id.back, R.id.restPass_bt_qd})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.restPass_bt_qd:
                text01 = mRestPassEt01.getText().toString();
                text02 = mRestPassEt02.getText().toString();
                if (text01.equals(text02)) {
                    requestData(YXHttpUrl.WD_GPASSWORD_GG,
                            UserSessionManager.getInstance().getUserId(), text01);
                } else {
                    ToastMaker.showToastShort(R.string.password_not_same);
                }
                break;
        }
    }

    public Dialog showDialog(final Context context, int type) {
        View view = LayoutInflater.from(context).inflate(R.layout.alert_dialogpass, null);

        final Dialog dialog = new AlertDialog.Builder(context).setView(view).create();

        dialog.show();
        TextView left = (TextView) view.findViewById(R.id.pass_text_left);
        TextView right = (TextView) view.findViewById(R.id.pass_text_right);
        TextView title = (TextView) view.findViewById(R.id.pass_text_tishi);
        TextView neirong = (TextView) view.findViewById(R.id.pass_text_neirong);
        TextView huitiao = (TextView) view.findViewById(R.id.pass_huitiao03);
        switch (type) {
            case 1:
                title.setText(getResources().getString(R.string.info));
                neirong.setText(getResources().getString(R.string.reset_success));
                left.setText(getResources().getString(R.string.iknow));
                right.setVisibility(View.GONE);
                huitiao.setVisibility(View.GONE);
                break;
            case 2:
                title.setText(getResources().getString(R.string.info));
                neirong.setText(getResources().getString(R.string.password_not_success));
                left.setText(getResources().getString(R.string.reset_giveup));
                right.setText(getResources().getString(R.string.reset_retry_again));
                break;
        }


        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        return dialog;
    }

    private void requestData(String url, String userId, String userPassword) {
        OkHttpUtils.post()
                .url(url)
                .addParams("userId", userId)
                .addParams("userPassword", userPassword)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMaker.showToastShort(R.string.generic_error);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        UserInfoResult result = gson.fromJson(response, UserInfoResult.class);
                        if (result != null && result.status == 200) {
                            UserSessionManager.updateUserInfo(result.data);
                            showDialog(ResetPasswordActivitye.this, 1);
                        } else {
                            showDialog(ResetPasswordActivitye.this, 2);
                        }
                    }
                });
    }
}
